<?php
load_info_var($cf_controller);											//加载smarty变量
load_file('menu_class',LIB_PATH);

$menu = new Menu($db);
$menu_array = $menu->load_data();
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;


switch($cf_action){
	case "index":
		$smarty->mypage($cf_controller,$cf_action,0) ;
	break;
	case "add":
		if($cf_ispost){
			$menu_title = $_POST['menu_title'];
			$menu_class = $_POST['menu_class'];
			$menu_link = $_POST['menu_link'];
			$menu_parent_id = intval($_POST['menu_parent_id']);
			$menu_order = intval($_POST['menu_order']);
			$menu_common = intval($_POST['menu_common']);
			$menu_target = $_POST['menu_target'];
			//属子栏目
			if(intval($menu_parent_id)){
				$row = $menu->get_data_row($menu_parent_id);
				$menu_level = $row['menu_level'] + 1;
			}else{
				$menu_level = 1;
			}
			$data = array(
				'menu_title'=>$menu_title,
				'menu_class'=>$menu_class,
				'menu_link'=>$menu_link,
				'menu_parent_id'=>$menu_parent_id,
				'menu_level'=>intval($menu_level),
				'menu_order'=>$menu_order,
				'menu_common'=>$menu_common,
				'menu_target'=>$menu_target
			);
			$result = $menu->insert_data($data);

			if($result){
				//更新菜单静态文件
				$menu->update_data_file();
                $return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
                unset($_SESSION['pre_url']);
				$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
				my_jump('添加成功'.$more,'',$return_page,3);
			}
			
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
			$menu_parent_id = intval($_GET['pid']);
			$row['menu_parent_id'] =  $menu_parent_id ? $menu_parent_id : '';
                        $_SESSION['pre_url']  = $cf_pre_page ? $cf_pre_page  : ''; 
		}
	break;
	case "edit":
		if($cf_ispost){

			$menu_title = $_POST['menu_title'];
			$menu_class = $_POST['menu_class'];
			$menu_link = $_POST['menu_link'];
			$menu_parent_id = intval($_POST['menu_parent_id']);
			$menu_order = intval($_POST['menu_order']);
			$menu_common = intval($_POST['menu_common']);
			$menu_target = $_POST['menu_target'];
			//属子栏目
			if(intval($menu_parent_id)){
				$row = $menu->get_data_row($menu_parent_id);
				$menu_level = $row['menu_level'] + 1;
			}else{
				$menu_level = 1;
			}
			$data = array(
				'menu_title'=>$menu_title,
				'menu_class'=>$menu_class,
				'menu_link'=>$menu_link,
				'menu_parent_id'=>$menu_parent_id,
				'menu_level'=>intval($menu_level),
				'menu_order'=>$menu_order,
				'menu_common'=>$menu_common,
				'menu_target'=>$menu_target
			);
			$result = $menu->update_data($data,"menu_id='$id'");
			
			if($result){
                            $return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
                            unset($_SESSION['pre_url']);
				//更新菜单静态文件
				$menu->update_data_file();
				my_jump('修改成功','',$return_page,1);
			}else{
                            my_jump('修改失败');
                        }
				
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
			$id = intval($_GET['id']);
			$row = $menu->get_data_row($id);
			if(!$row)
			my_jump('沒有此分類信息');

                        $_SESSION['pre_url']  = $cf_pre_page ? $cf_pre_page  : ''; 
		}
	break;
	case "del":
		$id = intval($_GET['id']);
		//$db->debug(1,1);
		$result = $menu->del_data("menu_id='$id'");
		
		if($result){
			//更新菜单静态文件
			$menu->update_data_file();
			my_jump('删除成功','',$return_page,1);
		}else{
			my_jump('删除失败!');
		}
		
	break;
	case "set":
		$type = $_GET['type'];
		$value = $_GET['value'];
		$where = "menu_id = '".$id."'";
		$result = $menu->set_data($value,$type,$where);
		if($result){
			//更新菜单静态文件
			$menu->update_data_file();
			my_jump('更新完成','',$return_page,1);
		}else{ 
			my_jump('更新失败');
		}
	break;
	default:
		my_jump('非法操作!');
	break;
	
	
	
}

$smarty->assign(array(
	'id'=>$id,
	'row'=>htmlchar($row),
	'menu_array'=>$menu_array,

));
$smarty->mydisplay();
?>